/*
 * @Description: 
 * @Author: Tian Hongcheng
 * @Date: 2022-03-20 21:07:39
 * @LastEditTime: 2022-03-20 21:56:15
 * @LastEditors: Tian Hongcheng
 * @Reference: 
 */
package com.reins.bookstore.controller;
import com.reins.bookstore.constant.Constant;
import com.reins.bookstore.entity.Book;
import com.reins.bookstore.object.BookDTO;
import com.reins.bookstore.object.BookRate;
import com.reins.bookstore.object.Filter;
import com.reins.bookstore.object.UserRate;
import com.reins.bookstore.service.BookService;
import com.reins.bookstore.utils.msgutils.Msg;
import com.reins.bookstore.utils.msgutils.MsgCode;
import com.reins.bookstore.utils.msgutils.MsgUtil;
import com.reins.bookstore.utils.pageutils.Page;
import com.reins.bookstore.utils.pageutils.PageUtil;
import com.reins.bookstore.utils.sessionutils.SessionUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/getAllBooks")
    public List<BookDTO> getBooks() {
        return bookService.getBooks();
    }

    @RequestMapping("/getAllBooksByKey")
    public Page getBooks(@RequestBody Map<String , String> params) {
        Integer now_page = Integer.parseInt(params.get("now_page"));
        String key = params.get("key");
        List<BookDTO> get = bookService.getBooks(key);

        net.sf.json.JSONArray data = net.sf.json.JSONArray.fromObject(get);
        Page result = PageUtil.makePage(now_page,data);

        return result;
    }

    @RequestMapping("/getBookById")
    public BookDTO getBook(@RequestParam("id") Integer id){
        return bookService.findBookById(id);
    }

    @RequestMapping("/saveOneBook")
    public BookDTO saveOneBook(@RequestBody Book add){return bookService.saveOneBook(add);}

//    @RequestMapping("/getBookRate")
//    public List<BookRate> getUserRate(@RequestBody Filter params){
//        return bookService.getBookRate(params,0);
//    }
//    @RequestMapping("/getBookRateById")
//    public List<BookRate> getUserRate(@RequestBody Filter params,@RequestParam("userId")Integer userId){
//        return bookService.getBookRate(params,userId);
//    }
//
//    @RequestMapping("/getAllBookRate")
//    public List<BookRate> getAllUserRate(){
//        return bookService.getBookRate(null,0);
//    }
//    @RequestMapping("/getAllBookRateById")
//    public List<BookRate> getAllUserRate(@RequestParam("userId")Integer userId)
//    {
//        return bookService.getBookRate(null,userId);
//    }
}
